{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "b1a81eb6",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np  \n",
    "import pandas as pd\n",
    "import matplotlib\n",
    "import matplotlib.pyplot as plt\n",
    "from matplotlib.ticker import MaxNLocator\n",
    "import requests\n",
    "import json\n",
    "# from PIL import Image\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f0f3e048",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "======数据保存在目录：E:/人数采集3.0/======\n",
      "======数据爬取成功======\n"
     ]
    }
   ],
   "source": [
    "import requests,os\n",
    "import re\n",
    "import xlwt\n",
    "import time\n",
    "import json\n",
    "\n",
    "class get_yq_info:\n",
    "\n",
    "    def get_data_html(self):\n",
    "            headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.163 Safari/535.1'}\n",
    "            response = requests.get('https://ncov.dxy.cn/ncovh5/view/pneumonia?from=timeline&isappinstalled=0', headers=headers, timeout=3)\n",
    "            # 请求页面\n",
    "\n",
    "            response = str(response.content, 'utf-8')\n",
    "            # 中文重新编码\n",
    "            return response\n",
    "            #返回了HTML数据\n",
    "\n",
    "    def get_data_dictype(self):\n",
    "            areas_type_dic_raw = re.findall('try { window.getAreaStat = (.*?)}catch\\(e\\)',self.get_data_html())\n",
    "            areas_type_dic = json.loads(areas_type_dic_raw[0])\n",
    "            return areas_type_dic\n",
    "            #返回经过json转换过的字典化的数据\n",
    "    def save_data_to_excle(self):\n",
    "            self.make_dir()\n",
    "            #调用方法检查数据目录是否存在，不存在则创建数据文件夹\n",
    "            count = 2\n",
    "            #数据写入行数记录\n",
    "            newworkbook = xlwt.Workbook()\n",
    "            worksheet = newworkbook.add_sheet('all_data')\n",
    "            # 打开工作簿，创建工作表\n",
    "\n",
    "            worksheet.write(1, 2, '省份名称')\n",
    "            worksheet.write(1, 3, '省份简称或城市名称')\n",
    "            worksheet.write(1, 4, '确诊人数')\n",
    "            worksheet.write(1, 5, '疑似人数')\n",
    "            worksheet.write(1, 6, '治愈人数')\n",
    "            worksheet.write(1, 7, '死亡人数')\n",
    "            worksheet.write(1, 8, '地区ID编码')\n",
    "            #写入数据列标签\n",
    "\n",
    "            for province_data in self.get_data_dictype():\n",
    "                    provincename = province_data['provinceName']\n",
    "                    provinceshortName = province_data['provinceShortName']\n",
    "                    p_confirmedcount = province_data['confirmedCount']\n",
    "                    p_suspectedcount = province_data['suspectedCount']\n",
    "                    p_curedcount = province_data['curedCount']\n",
    "                    p_deadcount = province_data['deadCount']\n",
    "                    p_locationid = province_data['locationId']\n",
    "                    #用循环获取省级以及该省以下城市的数据\n",
    "\n",
    "                    worksheet.write(count, 2, provincename)\n",
    "                    worksheet.write(count, 3, provinceshortName)\n",
    "                    worksheet.write(count, 4, p_confirmedcount)\n",
    "                    worksheet.write(count, 5, p_suspectedcount)\n",
    "                    worksheet.write(count, 6, p_curedcount)\n",
    "                    worksheet.write(count, 7, p_deadcount)\n",
    "                    worksheet.write(count, 8, p_locationid)\n",
    "                    #在工作表里写入省级数据\n",
    "\n",
    "                    count += 1\n",
    "                    #此处为写入行数累加，province部分循环\n",
    "\n",
    "                    for citiy_data in province_data['cities']:\n",
    "                            cityname = citiy_data['cityName']\n",
    "                            c_confirmedcount = citiy_data['confirmedCount']\n",
    "                            c_suspectedcount = citiy_data['suspectedCount']\n",
    "                            c_curedcount = citiy_data['curedCount']\n",
    "                            c_deadcount = citiy_data['deadCount']\n",
    "                            c_locationid = citiy_data['locationId']\n",
    "                            #该部分获取某个省下某城市的数据\n",
    "\n",
    "                            worksheet.write(count, 3, cityname)\n",
    "                            worksheet.write(count, 4, c_confirmedcount)\n",
    "                            worksheet.write(count, 5, c_suspectedcount)\n",
    "                            worksheet.write(count, 6, c_curedcount)\n",
    "                            worksheet.write(count, 7, c_deadcount)\n",
    "                            worksheet.write(count, 8, c_locationid)\n",
    "                            #该部分在工作表里写入某城市的数据\n",
    "\n",
    "                            count += 1\n",
    "                            #此处为写入行数累加，cities部分循环\n",
    "            current_time = time.strftime(\"%Y-%m-%d-%H-%M-%S\", time.localtime())\n",
    "            newworkbook.save('E:\\人数采集3.0\\实时采集v3.0-%s.xls' % (current_time))\n",
    "            print('======数据爬取成功======')\n",
    "    def make_dir(self):\n",
    "            file_path = 'E:/人数采集3.0/'\n",
    "            if not os.path.exists(file_path):\n",
    "                    os.makedirs(file_path)\n",
    "                    print('======数据文件夹不存在=======')\n",
    "                    print('======数据文件夹创建成功======')\n",
    "                    print('======创建目录为%s======'%(file_path))\n",
    "            else:\n",
    "                    print('======数据保存在目录：%s======' % (file_path))\n",
    "            #检查并创建数据目录\n",
    "    \n",
    "\n",
    "\n",
    "da = get_yq_info()\n",
    "da.get_data_html()\n",
    "da.save_data_to_excle()\n",
    "# data = da.get_data_dictype()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.2"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
